package com.qf.service.impl;

import com.qf.dao.GoodsDao;
import com.qf.dao.GoodsTypeDao;
import com.qf.dao.impl.GoodDaoImpl;
import com.qf.dao.impl.GoodsTypeDaoImpl;
import com.qf.domain.Goods;
import com.qf.domain.GoodsType;
import com.qf.domain.PageBean;
import com.qf.service.GoodTypeService;
import com.qf.service.GoodsService;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao = new GoodDaoImpl();
    private GoodsTypeDao goodsTypeDao=new GoodsTypeDaoImpl();
    @Override
    public PageBean<Goods> findByPage(int page_num, int page_size, String where, List<Object> params) {
        //public PageBean(int pageNum, int pageSize, long totalSize, List<T> data)
        long totalSize = goodsDao.getCount(where,params);
        List<Goods> data = goodsDao.selectByPage(page_num,page_size,where,params);
        PageBean<Goods> pageBean = new PageBean<Goods>(page_num,page_size,totalSize,data);
        return pageBean;
    }

    @Override
    public Goods findById(Integer gid) {
        Goods goods = goodsDao.selectById(gid);
        //根据商品类型id查找商品类型
        if (goods != null) {
            Integer typeid = goods.getTypeid();
            //调用业务层
            GoodTypeService goodTypeService = new GoodTypeServiceImpl();
            GoodsType goodType = goodTypeService.fingById(typeid);
            goods.setGoodsType(goodType);
        }
        return goods;
    }

    @Override
    public void add(Goods goods) {
        goodsDao.insert(goods);
    }
    @Override
    public List<Goods> findAll()  {
        List<Goods> goods = goodsDao.findAll();
        for (Goods good : goods) {
            GoodsType goodsType = goodsTypeDao.selectById(good.getTypeid());
            good.setGoodsType(goodsType);
        }
        return goods;
    }

    @Override
    public void deleteById(String gid)  {
        goodsDao.deleteById(gid);
    }

    @Override
    public void update(Goods goods)  {
        goodsDao.update(goods);
    }

    @Override
    public List<Goods> selectByUAD(StringBuilder where1, List<Object> i)  {
        List<Goods> goods = goodsDao.selectByUAD(where1, i);
        for (Goods good : goods) {
            GoodsType goodsType = goodsTypeDao.selectById(good.getTypeid());
            good.setGoodsType(goodsType);
        }
        return goods;
    }

}
